﻿/**************************************************************************
创建时间:	2020/5/18 15:04:03    
作	  者:	张存
邮 	  箱:	zhangcunliang@126.com

Copyright (c) zhcun.cn

描	述：
记	录：
***************************************************************************/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ZhCun.Win
{
    public partial class FrmSelectIcon : FrmBaseModal
    {
        public FrmSelectIcon()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 选中后触发
        /// </summary>
        public Action<string> OnSelected;

        /// <summary>
        /// 图标文件完整根路径
        /// </summary>
        string _IconFilePath;

        void InitFolderTree()
        {
            tvFolder.Nodes.Clear();
            TreeNode rootNode = new TreeNode();
            rootNode.Text = "主目录";
            rootNode.Name = _IconFilePath;
            tvFolder.Nodes.Add(rootNode);
            LoadSubFolder(rootNode);
            LoadImageFiles(rootNode);
            rootNode.Expand();
        }

        void LoadSubFolder(TreeNode node)
        {
            string path = node.Name;
            DirectoryInfo dir = new DirectoryInfo(path);
            var subDirs = dir.GetDirectories();
            if (subDirs != null)
            {
                foreach (var sDir in subDirs)
                {
                    TreeNode tn = new TreeNode();
                    tn.Text = sDir.Name;
                    tn.Name = sDir.FullName; //Path.Combine(path, folder);
                    LoadSubFolder(tn);
                    node.Nodes.Add(tn);
                }
            }
        }

        void LoadImageFiles(TreeNode node)
        {
            imgListIcon.Images.Clear();
            lvIcon.Items.Clear();
            string path = node.Name;
            DirectoryInfo dir = new DirectoryInfo(path);
            FileInfo[] fileList = dir.GetFiles("*.png");
            foreach (var file in fileList)
            {
                imgListIcon.Images.Add(Image.FromFile(file.FullName));
                var lvi = new ListViewItem();
                lvi.ImageIndex = imgListIcon.Images.Count - 1;
                lvi.Text = file.Name;
                lvi.Name = file.FullName;
                lvIcon.Items.Add(lvi);
            }
        }

        private void FrmSelectIcon_Load(object sender, EventArgs e)
        {
            _IconFilePath = Path.Combine(Application.StartupPath, Config.Settings.IconFolder);
            lvIcon.LargeImageList = imgListIcon;
            tvFolder.NodeMouseClick += TvFolder_NodeMouseClick;
            lvIcon.MouseDoubleClick += LV_MouseDoubleClick;
            InitFolderTree();
        }

        private void LV_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            tsBtnSelected_Click(sender, e);
        }

        private void TvFolder_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            if (e.Node == tvFolder.SelectedNode) return;

            LoadImageFiles(e.Node);
        }

        private void tsBtnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void tsBtnRefresh_Click(object sender, EventArgs e)
        {
            InitFolderTree();
        }

        private void tsBtnSelected_Click(object sender, EventArgs e)
        {
            if (lvIcon.SelectedItems.Count > 0)
            {
                ListViewItem lvi = lvIcon.SelectedItems[0];

                string saveFile = lvi.Name.Remove(0, _IconFilePath.Length + 1);
                OnSelected?.Invoke(saveFile);
                this.DialogResult = DialogResult.OK;
            }
        }
    }
}
